package com.itboye.ihomebank.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.itboye.ihomebank.R;

import java.util.Random;

/**
 * 弧形进度条
 */
public class RxRoundProgress extends View {
    /**
     * 进度条画笔
     */
    Paint paint;//外圆环
    Paint paint01;//内圆环
    Paint paintText;
    /**
     * 圆环的宽度
     */
    private float roundWidth;
    /**
     * 圆环的颜色
     */
    private int roundColor;

    /**
     * 圆环进度的颜色
     */
    private int roundProgressColor;
    /**
     * 中间进度百分比的字符串的颜色
     */
    private int textColor;
    /**
     * 中间进度百分比的字符串的字体
     */
    private float textSize;

    /**
     * 当前进度
     */
    private double progress;

    /**
     * 最大进度
     */
    private double max;
    public RxRoundProgress(Context context) {
        this(context, null);
    }
    public RxRoundProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RxRoundProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint=new Paint();//外圆环画笔
        paint01=new Paint();//内圆环画笔
        paintText=new Paint();//进度条的画笔
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RxRoundProgress);
        roundColor = mTypedArray.getColor(R.styleable.RxRoundProgress_roundColor, Color.WHITE);
        roundWidth = mTypedArray.getDimension(R.styleable.RxRoundProgress_roundWidth, 20);
        roundProgressColor = mTypedArray.getColor(R.styleable.RxRoundProgress_roundProgressColor, Color.parseColor("#F6B141"));
        textColor = mTypedArray.getColor(R.styleable.RxRoundProgress_textColor, Color.GREEN);
        textSize = mTypedArray.getDimension(R.styleable.RxRoundProgress_textSize1, 25);
        max = mTypedArray.getInteger(R.styleable.RxRoundProgress_max, 100);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(roundColor); //设置圆环的颜色
        paint.setAntiAlias(true);//消除锯齿
        paint.setStyle(Paint.Style.STROKE);//设置空心
        paint.setStrokeWidth(roundWidth);//设置圆环的宽度

        /**
         * 画最外层的大圆环
         */
        int centre = getWidth()/2-90; //获取圆心的x坐标
        int radius = (int) (centre - roundWidth/2); //圆环的半径
        int left=centre-radius+90;
        int top=centre+radius+90;
        RectF oval=new RectF(left,left,top,top); //用于定义的圆弧的形状和大小的界限
        /**
         * 根据进度画圆弧
         * •oval  指定圆弧的外轮廓矩形区域。
         * •startAngle: 圆弧起始角度，单位为度。
         * •sweepAngle: 圆弧扫过的角度，顺时针方向，单位为度,从右中间开始为零度。
         * •useCenter: 如果为True时，在绘制圆弧时将圆心包括在内，通常用来绘制扇形,位false则为圆弧
         */
        canvas.drawArc(oval,90,360,false,paint);


        /**
         * 画内部的大圆环
         */
        paint01.setStrokeWidth(0);
        paint01.setTextSize(textSize);
        paint01.setTypeface(Typeface.DEFAULT_BOLD); //设置字体

        //设置进度是实心还是空心
        paint01.setStrokeWidth(roundWidth); //设置圆环的宽度
        paint01.setAntiAlias(true);//消除锯齿
        paint01.setStyle(Paint.Style.STROKE);//设置空心
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_banner_my);
        paint01.setShader(new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR));//设置进度的颜色

        /**
         * 画进度百分比
         */
        int percent = (int)(((float)progress / (float)max) * 100);  //中间的进度百分比，先转换成float在进行除法运算，不然都为0
        float textWidth = paint01.measureText(percent + "%");   //测量字体宽度，我们需要根据字体的宽度设置在圆环中间
        paintText.setColor(textColor);
        paintText.setTextSize(textSize);

        if(progress>=0){
            canvas.drawArc(oval, 90, 360* ((float)progress / (float)max), false, paint01);  //根据进度画圆弧
            canvas.drawText(percent + "%", centre+90 - textWidth / 2, centre + 90 + textSize/2, paintText); //画出进度百分比
        }
    }
    public synchronized double getMax() {
        return max;
    }
    /**
     * 获取进度.需要同步
     * @return
     */
    public synchronized double getProgress() {
        return progress;
    }
    /**
     * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步
     * 刷新界面调用postInvalidate()能在非UI线程刷新
     * @param progress
     */
    public synchronized void setProgress(double progress) {
            this.progress = progress;
            postInvalidate();
    }

    /**
     * 随机颜色
     * @return
     */
    public static int randomColor() {
        Random random = new Random();
        int red = random.nextInt(250) + 50;
        int green = random.nextInt(150) + 50;
        int blue = random.nextInt(350) + 50;
        return Color.rgb(red, green, blue);
    }
}
